<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\User\Ctl;

use Srv\Apps\Apis\Common\IdxCtl\IdxIp;
use Srv\Apps\Proto\DTO\User\ReviewFeedbackListDTO;
use Srv\Apps\Proto\Enum\ReviewFeedbackScenesEnum;
use Srv\Apps\Proto\Enum\YesOrNoEnum;
use Srv\Apps\Proto\VO\User\ReviewFeedbackListVO;
use Srv\Apps\Proto\VO\User\ReviewFeedbackVO;
use Srv\Apps\Proto\Enum\StarResultEnum;
use Srv\Apps\Proto\VO\User\UserInfoBasicVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;
use Srv\Libs\Tools\IpAddr;

final class ReviewFeedbackCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write
    public array $scenesList                = [
        ReviewFeedbackScenesEnum::REVIEW_FEEDBACK_SCENES_UNKNOWN    => '未知',
        ReviewFeedbackScenesEnum::REVIEW_FEEDBACK_SCENES_SHARE      => '分享',
        ReviewFeedbackScenesEnum::REVIEW_FEEDBACK_SCENES_UNLOCK     => '解锁',
        ReviewFeedbackScenesEnum::REVIEW_FEEDBACK_SCENES_TASK_PULL  => '领福利'
    ];

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $scenes
     * @return string
     * getReviewFeedBackScenes
     */
    public function getReviewFeedBackScenes(int $scenes):string
    {
        return $this->scenesList[$scenes]??'未知';
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return ReviewFeedbackListVO|null
     * getReviewFeedbackList
     */
    public function getReviewFeedbackList(Page $Page, array $whereData):?ReviewFeedbackListVO
    {
        $limitString        = $Page->getLimitString();
        $whereString        = ' WHERE rwfr_delete=0 ';
        $whereInfoString    = ' WHERE usi_delete=0';
        $infoSearch         = false;
        // 检索条件
        if(count($whereData) > 0) foreach ($whereData as $key => $val){
            switch ($key){
                case ReviewFeedbackListDTO::_userId:
                    $whereString            .= (strlen($whereString)>0?' AND ':'').'user_id='. intval($val);
                    break;
                case ReviewFeedbackListDTO::_idNum:
                    if(strlen($val) > 10 && is_numeric($val)){
                        $whereString        .= (strlen($whereString)>0?' AND ':'').'user_id='. intval($val);
                    }else{
                        $infoSearch         = true;
                        $whereInfoString    .= (strlen($whereInfoString)>0?' AND ':'').'usi_idnum LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    }
                    break;
                case ReviewFeedbackListDTO::_pullStatus:
                    $whereString            .= (strlen($whereString)>0?' AND ':'').'rwfr_pull_status='. intval($val);
                    break;
                case ReviewFeedbackListDTO::_scenes:
                    $whereString            .= (strlen($whereString)>0?' AND ':'').'rwfr_scenes='. intval($val);
                    break;
                case ReviewFeedbackListDTO::_close:
                    $whereString            .= (strlen($whereString)>0?' AND ':'').'rwfr_close='. intval($val);
                    break;
                case ReviewFeedbackListDTO::_starResult:
                    $whereString            .= (strlen($whereString)>0?' AND ':'').'rwfr_star_result='. intval($val);
                    break;
                case ReviewFeedbackListDTO::_contentStatus:
                    $whereString            .= (strlen($whereString)>0?' AND ':'').'rwfr_content_status='. intval($val);
                    break;
                case ReviewFeedbackListDTO::_createTimeStart:
                    $whereString        .= (strlen($whereString)>0?' AND ':'').'rwfr_create_time>='. intval($val);
                    break;
                case ReviewFeedbackListDTO::_createTimeStop:
                    $whereString        .= (strlen($whereString)>0?' AND ':'').'rwfr_create_time<='. intval($val);
                    break;
            }
        }
        if($infoSearch){
            $subSql         = 'SELECT user_id FROM '.$this->DbAuto->getTableNameAll('user_info').$whereInfoString;
            $whereString    .=  (strlen($whereString)>0?' AND ':'').'user_id IN('.$subSql.')';
        }
        $isFail             = false;
        $sql                = 'SELECT SQL_CALC_FOUND_ROWS rwfr_id, user_id, rwfr_version, rwfr_ip, rwfr_scenes, rwfr_close, rwfr_star_num, rwfr_star_result, rwfr_content_status, rwfr_content_text, rwfr_reward_num, rwfr_reward_time, rwfr_pull_status, rwfr_pull_time, rwfr_create_time, rwfr_update_time FROM '.$this->DbAuto->getTableNameAll('review_feedback_record').$whereString.' ORDER BY rwfr_id DESC '.$limitString;
        $list               = $this->DbAuto->getData($sql, $isFail);
        $totalNum           = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $ReviewFeedbackVOList       = [];
        if(CommArray::isForeach($list)){
            $UserCtl                = UserCtl::tag();
            $userIdList             = array_unique(array_filter(array_map('intval', array_column($list, 'user_id'))));
            $UserInfoBasicVOList    = $UserCtl->getUserInfoBasicBatch($userIdList);
            foreach($list as $item){
                $rwfrIdItem                 = intval($item['rwfr_id']??0);
                if($rwfrIdItem < 1) continue;
                $userIdItem                 = intval($item['user_id']);
                if($userIdItem < 1) continue;
                $ReviewFeedbackVO           = $this->getReviewFeedbackVO($item);
                if(!($ReviewFeedbackVO instanceof ReviewFeedbackVO)) continue;
                $UserInfoBasicVO            = $UserInfoBasicVOList[$userIdItem]??null;
                if(!($UserInfoBasicVO instanceof UserInfoBasicVO)) continue;
                $ReviewFeedbackVO->setUserInfoBasic($UserInfoBasicVO);
                $ReviewFeedbackVOList[]     = $ReviewFeedbackVO;
            }
        }
        $ReviewFeedbackListVO       = new ReviewFeedbackListVO();
        $ReviewFeedbackListVO->setList($ReviewFeedbackVOList);
        $ReviewFeedbackListVO->setPage($Page->getPageVO($totalNum));
        return $ReviewFeedbackListVO;
    }

    /**
     * @param array $info
     * @return ReviewFeedbackVO|null
     * getReviewFeedbackVO
     */
    public function getReviewFeedbackVO(array $info):?ReviewFeedbackVO{
        $rwfrId             = intval($info['rwfr_id']??0);
        if($rwfrId < 1) return null;
        $ReviewFeedbackVO   = new ReviewFeedbackVO();
        $ReviewFeedbackVO->setRwfrId($rwfrId);
        $ReviewFeedbackVO->setUserId(intval($info['user_id']));
        $ReviewFeedbackVO->setVersion(trim($info['rwfr_version']));
        $ipString           = IdxIp::tag()->getIpStrByIntStr($info['rwfr_ip']);
        $ipArea             = IpAddr::getLocationInfo($ipString);
        $ReviewFeedbackVO->setIpStr($ipString.'('.($ipArea['country']??'').')');
        $ReviewFeedbackVO->setScenes(ReviewFeedbackScenesEnum::parseValue($info['rwfr_scenes']));
        $ReviewFeedbackVO->setClose(YesOrNoEnum::parseValue($info['rwfr_close']));
        $ReviewFeedbackVO->setStarNum(intval($info['rwfr_star_num']));
        $ReviewFeedbackVO->setStarResult(StarResultEnum::parseValue($info['rwfr_star_result']));
        $ReviewFeedbackVO->setContentStatus(YesOrNoEnum::parseValue($info['rwfr_content_status']));
        $ReviewFeedbackVO->setContentText(trim($info['rwfr_content_text']));
        $ReviewFeedbackVO->setRewardNum(intval($info['rwfr_reward_num']));
        $ReviewFeedbackVO->setRewardTime(intval($info['rwfr_reward_time']));
        $ReviewFeedbackVO->setPullStatus(YesOrNoEnum::parseValue($info['rwfr_pull_status']));
        $ReviewFeedbackVO->setPullTime(intval($info['rwfr_pull_time']));
        $ReviewFeedbackVO->setCreateTime(intval($info['rwfr_create_time']));
        $ReviewFeedbackVO->setUpdateTime(intval($info['rwfr_update_time']));
        return $ReviewFeedbackVO;
    }
}